/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.optimus.web.listeners;

import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.optimus.common.util.Constants;
import com.optimus.app.Application;
import com.optimus.db.DBFactory;

/**
 * Web application lifecycle listener.
 * @author vperez
 */

public class WebAppListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        try {
            String log4jConfigFile = sce.getServletContext().getInitParameter(Constants.LOG4J_XML_FILE);
            String hibernateConfigFile = sce.getServletContext().getInitParameter(Constants.HIBERNATE_XML_FILE);
            String logLevel = sce.getServletContext().getInitParameter(Constants.APPLICATION_LOG_LEVEL);
            String logFile = sce.getServletContext().getInitParameter(Constants.APPLICATION_LOG_FILE);
            System.getProperties().setProperty(Constants.APPLICATION_LOG_LEVEL, logLevel);
            System.getProperties().setProperty(Constants.APPLICATION_LOG_FILE, logFile);
            URL archivo = sce.getServletContext().getResource(log4jConfigFile);
            Application.instance().configureLog4j(archivo);
            URL archivo2 = sce.getServletContext().getResource(hibernateConfigFile);
            DBFactory.instance().currentFactory(archivo2);
        } catch (Exception ex) {
            Logger.getLogger(WebAppListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}